package com.alliky.core.net;

import android.content.Context;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.alliky.core.R;
import com.alliky.core.api.BaseApi;
import com.alliky.core.utils.ConvertUtils;
import com.alliky.core.utils.GlideRoundTransform;
import com.alliky.core.utils.RoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.squareup.picasso.Picasso;

import static com.alliky.core.utils.StringUtils.isNotEmpty;

/**
 * ================================================
 *
 * @Description: 描述  Picasso 方式加载图片
 * @Author: wxianing
 * @Date: 2022/11/1 10:19
 * ================================================
 */
public class ImageLoader {

    //默认圆角大小
    private static int RADIUS = 2;
    //默认图(加载过程中或加载失败显示的图片)
    private static int DEFAULT_RESID = R.mipmap.icon_default;

    private static String getUrl(String path) {
        if (isNotEmpty(path) && path.startsWith("http")) {
            return path;
        } else {
            return BaseApi.BASE_IMAGE + path;
        }
    }

    public static void load(String url, ImageView imageView) {
        load(url, imageView, DEFAULT_RESID);
    }

    public static void load(String url, ImageView imageView, @DrawableRes int resId) {
        load(url, imageView, resId, RADIUS);
    }

    public static void load(String url, ImageView imageView, float radius) {
        load(url, imageView, DEFAULT_RESID, radius);
    }

    public static void load(String url, ImageView imageView, @DrawableRes int resId, float radius) {
        Picasso.get()
                .load(getUrl(url))
                .placeholder(resId)
                .error(resId)
                .transform(new RoundTransform(radius))
                .into(imageView);
    }

    public static void display(Context context, String url, ImageView imageView) {
        display(context, url, imageView, DEFAULT_RESID);
    }

    public static void display(Context context, String url, ImageView imageView, @DrawableRes int resId) {
        display(context, url, imageView, resId, RADIUS);
    }

    public static void display(Context context, String url, ImageView imageView, float radius) {
        display(context, url, imageView, DEFAULT_RESID, radius);
    }

    public static void display(Context context, String url, ImageView imageView, @DrawableRes int resId, float radius) {
        Glide.with(context).load(getUrl(url))
                .apply(new RequestOptions()
                        .placeholder(resId)
                        .error(resId)
                        .dontAnimate()
                        .centerCrop()
                        .transform(new GlideRoundTransform(context, ConvertUtils.px2dp(radius))))
                .into(imageView);
    }
}
